﻿using System;
using System.Collections.Generic;
using System.Text;

namespace FiveStone.MyStack
{
    /// <summary>
    /// 棋子记录
    /// </summary>
    public class MyStack
    {        
        //public const int MAXSIZE = 5000;
        public struct FSStack
        {
            //public string[] data;            
            public IList<Chess> HistoryPoint;
            public int top;
        }

        public FSStack st = new FSStack();

        public void CreatStack()
        {
            //st.data = new string[MAXSIZE];
            st.HistoryPoint = new List<Chess>();
            st.top = -1;
        }

        /// <summary>
        /// 增加一条下棋记录到末尾
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public bool Insert(int x,int y)
        {
            //string str = String.Format("X：{0} Y：{1}", x, y);
            //if (st.top == MAXSIZE - 1)
            //{
            //    return false;
            //}
            //else
            //{
                st.top++;
                //st.data[st.top] = str;
                st.HistoryPoint.Add(new Chess(x,y));                
                return true;
            //}
        }

        /// <summary>
        /// 删除末条记录
        /// </summary>
        /// <returns></returns>
        public bool Delete()
        {
            if (st.top == -1)
            {
                return false;
            }
            else
            {                
                st.HistoryPoint.RemoveAt(--st.top);
                //st.top--;
                return true;
            }
        }

        #region 旧代码/已未使用
        public string GetTop()
        {
            return st.HistoryPoint[st.top].ToString();
        }

        /// <summary>
        /// 得到电脑本次下棋位置
        /// </summary>
        /// <returns></returns>
        public string GetPC()
        {
            if (st.top != -1)
            {
                return "PC:" + (st.HistoryPoint[st.top].ToString());
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 得到人本次下棋位置
        /// </summary>
        /// <returns></returns>
        public string GetPerson()
        {
            if (st.top != -1&&st.top>0)
            {
                return "我:" + (st.HistoryPoint[st.top - 1].ToString());
            }
            else
            {
                return "";
            }
        }
        #endregion

        /// <summary>
        /// 得到本次下棋记录
        /// </summary>
        /// <returns></returns>
        public string GetGobangRecord(string PlayerName)
        {
            if (st.top != -1)
            {
                //return String.Format("[{0}] {1}:{2}",st.top+1, PlayerName, (st.HistoryPoint[st.top].ToString()));
                return String.Format("[{0}] {1} {2}", st.top + 1, PlayerName.Trim(), (st.HistoryPoint[st.top].ToString()));
            }
            else
            {
                return String.Empty;
            }
        }
    }
}
